import { hasAuthority } from '@/utils/authority-utils'
import { loginIgnore } from '@/router/index'
// import { checkAuthorization } from '@/utils/request'
// import { getRoutesConfig } from "@/services/user";
// import { loadRoutes } from "@/utils/routerUtil";
import { checkRole } from '@/services/user';
import NProgress from 'nprogress'
NProgress.configure({ showSpinner: false })

/**
 * 进度条开始
 * @param to
 * @param form
 * @param next
 */
const progressStart = (to, from, next) => {
  // start progress bar
  if (!NProgress.isStarted()) {
    NProgress.start()
  }
  next()
}





/**
 * 登录守卫
 * @param to
 * @param form
 * @param next
 * @param options
 */
const loginGuard = async (to, from, next, options) => {
 
  const { message, store } = options
  if (loginIgnore.includes(to)) {  //白名单
    next()
  } else if (!Object.keys(store.getters['account/getHeader']).length > 0) {   //没有header

    message.warning('请先登录')
      next({
      path: '/login', // 未登录则跳转至login页面 
      query: {redirect: to.fullPath,
      } // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面 
    })
    NProgress.done()   //避免后退卡死 结束进度条动画
  } else {
    if (!store.getters['account/first']) {   //刷新页面重新获取
      await checkRole().then((res) => {
        if (res.code == 200) {
          const { user, permissions } = res.data;
          store.commit('account/setUser', user);                //更新用户信息
          store.commit('account/setFirst', true);               //点击栏目时不刷新
          store.commit('account/setPermissions', permissions);  //更新权限信息
        }
      })
    }
    next()
  }
}

/**
 * 权限守卫
 * @param to
 * @param form
 * @param next
 * @param options
 */
const authorityGuard = (to, from, next, options) => {
  const { store, message } = options
  const permissions = store.getters['account/permissions']
  const roles = store.getters['account/roles']
  if (!hasAuthority(to, permissions, roles)) {
    message.warning(`对不起，您无权访问页面: ${to.fullPath}，请联系管理员`)
    next({ path: '/403' })
  } else {
    next()
  }
}

/**
 * 混合导航模式下一级菜单跳转重定向
 * @param to
 * @param from
 * @param next
 * @param options
 * @returns {*}
 */
const redirectGuard = (to, from, next, options) => {
  const { store } = options
  const getFirstChild = (routes) => {
    const route = routes[0]
    if (!route.children || route.children.length === 0) {
      return route
    }
    return getFirstChild(route.children)
  }
  if (store.state.setting.layout === 'mix') {
    const firstMenu = store.getters['setting/firstMenu']
    if (firstMenu.find(item => item.fullPath === to.fullPath)) {
      store.commit('setting/setActivatedFirst', to.fullPath)
      const subMenu = store.getters['setting/subMenu']
      if (subMenu.length > 0) {
        const redirect = getFirstChild(subMenu)
        return next({ path: redirect.fullPath })
      }
    }
  }
  next()
}

/**
 * 进度条结束
 * @param to
 * @param form
 * @param options
 */
const progressDone = () => {
  // finish progress bar
  NProgress.done()
}

export default {
  beforeEach: [progressStart, loginGuard, authorityGuard, redirectGuard],
  afterEach: [progressDone]
}
